<?php

namespace app\api\controller\cps;

use app\api\model\UserOverlordMealOrder;
use app\common\controller\Api;
use fast\Http;
use think\Request;


/**
 * 美团霸王餐
 */
class Mtmeal extends Api
{
    protected $noNeedLogin = ['taskfeedback','cstaskfeedback'];
    protected $noNeedRight = ['*'];


    //订单
    public function orderlist()
    {
        $keyword = input('keyword','');

        $status = input('status','all');

        $where = ['user_id'=>$this->auth->id];

        if ($status!='all'){
            $where['status'] = $status;
        }

        if ($keyword){
            $where['order_id'] = ['like',"%$keyword%"];
        }

        $list = UserOverlordMealOrder::where($where)
            ->order('createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('success',$list);
    }

    //对账单
    public function accountstate()
    {
        try {
            $mt_model = new \app\api\model\cps\MtMeal();
            $res = $mt_model->accountstate();

        }  catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('请求成功',$res);
    }

    //活动商家列表
    public function shoplist()
    {
        try {
            $search = input('search','');
            $lat = input('lat','');
            $lng = input('lng','');
            $pagePvId = input('pagePvId','');
            $sortType = input('sortType',0);
            $canSignup = input('canSignup',0);
            $needEvaluate = input('needEvaluate',0);
            $mt_model = new \app\api\model\cps\MtMeal();

            $res = $mt_model->shoplist($this->auth->mobile,$search,$lat,$lng,$pagePvId,$sortType,$canSignup,$needEvaluate);

        }  catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('请求成功',$res);
    }

    //
    public function request()
    {
        $clickMonitorUrl = input('clickMonitorUrl','');
        Http::get($clickMonitorUrl);
        $this->success('请求成功');
    }

    //报名
    public function signup()
    {
        try {
            $poiEventId = input('poiEventId','');
            $meituanPvId = input('meituanPvId','');
            $lat = input('lat','');
            $lng = input('lng','');
            $mobile = input('mobile','');
            $shop_name = input('shop_name','');
            $shop_logo = input('shop_logo','');
            $wxAppid = input('wxAppid','');
            $wxPath = input('wxPath','');
            $planActivityType = input('planActivityType',0);
            $mt_model = new \app\api\model\cps\MtMeal();
            $res = $mt_model->signup($this->auth->id,$mobile,$poiEventId,$meituanPvId,$lat,$lng,$shop_name,$shop_logo,$wxAppid,$wxPath,$planActivityType);

        }  catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('请求成功',$res);
    }

    //根据商家活动id查询是否报名
    public function issignup()
    {
        $poiEventId = input('poiEventId','');

        $exist = UserOverlordMealOrder::where(['user_id'=>$this->auth->id,'poiEventId'=>$poiEventId])
            ->where('status','not in',[-1,2])->find();

        $this->success('请求成功',$exist);
    }

    //报名任务状态
    public function taskstatus()
    {
        try {
            $userEventId = input('userEventId','');
            $mt_model = new \app\api\model\cps\MtMeal();
            $res = $mt_model->taskstatus($userEventId);

        }  catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('请求成功',$res);
    }

    //取消报名
    public function cancel()
    {
        try {
            $order_no = input('order_no','');
            $mt_model = new \app\api\model\cps\MtMeal();
            $res = $mt_model->cancel($order_no);

        }  catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('请求成功',$res);
    }


    //任务状态反馈回调
    public function taskfeedback()
    {
        try {
            $param = input();
            $mt_model = new \app\api\model\cps\MtMeal();
            $res = $mt_model->taskfeedback($param);
        }  catch (\Exception $e) {
            $this->error($e->getMessage(),'',1);
        }
        $this->success('请求成功',$res,0);
    }

    //上传图文反馈
    public function feedbackCreate()
    {
        $poiEventId = input('poiEventId','');
        $feedback_images = trim(input('feedback_images'), ',');

        $orderModel = UserOverlordMealOrder::where(['user_id'=>$this->auth->id,'poiEventId'=>$poiEventId])
            ->where('status','not in',[-1,2])->find();
        if (!$orderModel) {
            $this->error('订单不存在');
        }
        if (!$feedback_images) {
            $this->error('请上传图文反馈');
        }
        $orderModel->feedback_images = $feedback_images;
        $orderModel->save();

        $this->success('成功');
    }
}